package com.leoman.spinningmachine.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.leoman.helper.util.ToastUtil;
import com.leoman.helper.view.LazyFagment;
import com.leoman.spinningmachine.http.APICallback;
import com.leoman.spinningmachine.http.APIResponse;
import com.leoman.spinningmachine.login.LoginActivity;
import com.leoman.spinningmachine.util.DialogUtils;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;

/**
 * Created by Administrator on 2017/4/10.
 * fragment基类
 */

public abstract class BaseFragment extends LazyFagment implements View.OnClickListener, BGARefreshLayout.BGARefreshLayoutDelegate, APICallback.OnResponseListener {

    private Unbinder unbinder;

    /**
     * bind layout resource file
     *
     * @return id of layout resource
     */
    protected abstract int getContentViewLayoutID();

    /**
     * init all views
     */
    protected abstract void initView(View view);

    /**
     * init all events
     */
    protected abstract void initData();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view;
        if (getContentViewLayoutID() != 0) {
            view = inflater.inflate(getContentViewLayoutID(), container, false);
        } else {
            throw new IllegalArgumentException("You must return a right contentView layout resource Id");
        }
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void lazyLoad() {

    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {

    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        return false;
    }

    @Override
    public void OnFailureData(String error, Integer tag) {
        DialogUtils.getInstance().cancel();
        if (!TextUtils.isEmpty(error)) {
            ToastUtil.showToast(getActivity(), "网络错误", Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void OnSuccessData(APIResponse apiResponse, Integer tag) {
        DialogUtils.getInstance().cancel();
    }

    @Override
    public void OnErrorData(String code, Integer tag) {
        DialogUtils.getInstance().cancel();
        if("100".equals(code)){
            SharedPreferencesUtils.getInstance().clear();
            ToastUtil.showToast(getActivity(),"登录已过期,请重新登录",Toast.LENGTH_SHORT);
            ((BaseActivity)getActivity()).gotoOtherActivity(LoginActivity.class);
        }else
            ToastUtil.showToast(getActivity(), code, Toast.LENGTH_SHORT);
    }

}
